package com.fyx.config;


import com.fyx.utils.StringUtils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.net.URLEncoder;

@Configuration
public class MultipleMongoConfig {
    @Autowired
    private MultipleMongoProperties mongoProperties;

    @Primary
    @Bean(name = com.fyx.config.PrimaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate primaryMongoTemplate() throws Exception {
        return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
    }

    @Bean
    @Qualifier(com.fyx.config.SecondaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate secondaryMongoTemplate() throws Exception {
        return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
    }

    @Bean
    @Qualifier(com.fyx.config.TertiaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate tertiaryMongoTemplate() throws Exception {
        return new MongoTemplate(tertiaryFactory(this.mongoProperties.getTertiary()));
    }

    @Bean
    @Qualifier(com.fyx.config.FourthMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate fourthMongoTemplate() throws Exception {
        return new MongoTemplate(fourthFactory(this.mongoProperties.getFourth()));
    }

    @Bean
    @Qualifier(com.fyx.config.FiveMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate fiveMongoTemplate() throws Exception {
        return new MongoTemplate(fiveFactory(this.mongoProperties.getFive()));
    }

    @Bean
    @Qualifier(com.fyx.config.SixthMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate sixthMongoTemplate() throws Exception {
        return new MongoTemplate(sixthFactory(this.mongoProperties.getSixth()));
    }

    @Bean
    @Qualifier(com.fyx.config.SeventhMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate seventhMongoTemplate() throws Exception {
        return new MongoTemplate(seventhFactory(this.mongoProperties.getSeventh()));
    }

    @Bean
    @Qualifier(com.fyx.config.EighthMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate eighthMongoTemplate() throws Exception {
        return new MongoTemplate(eighthFactory(this.mongoProperties.getEighth()));
    }

    @Bean
    @Qualifier(com.fyx.config.NinethMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate ninethMongoTemplate() throws Exception {
        return new MongoTemplate(ninethFactory(this.mongoProperties.getNineth()));
    }

    @Bean
    @Qualifier(com.fyx.config.TenthMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate tenthMongoTemplate() throws Exception {
        return new MongoTemplate(tenthFactory(this.mongoProperties.getTenth()));
    }

    @Bean
    @Qualifier(com.fyx.config.EleventhMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate eleventhMongoTemplate() throws Exception {
        return new MongoTemplate(eleventhFactory(this.mongoProperties.getEleventh()));
    }

    @Bean
    @Primary
    public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory tertiaryFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory fourthFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory fiveFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory sixthFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory seventhFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory eighthFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory ninethFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory tenthFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }

    @Bean
    public MongoDbFactory eleventhFactory(MongoProperties mongo) throws Exception {
        if (StringUtils.isEmpty(mongo.getUsername()) || mongo.getPassword() == null ) {
            return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                    mongo.getDatabase());
        } else {
            String password = URLEncoder.encode(String.valueOf(mongo.getPassword()));
            String url = "mongodb://" + mongo.getUsername()
                    + ":" + password
                    + "@" + mongo.getHost() + ":" + mongo.getPort() + "/" + mongo.getDatabase()
                    + "?authSource="+mongo.getAuthenticationDatabase()+"&authMechanism=SCRAM-SHA-1";
            return new SimpleMongoDbFactory(new MongoClientURI(url));
        }
    }
}
